package com.ruoyi.common.tools.excel.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DeptService;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.core.service.PostService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.employee.domain.vo.EmployeeImportDetailVo;
import com.ruoyi.erecord.employee.service.IEmployeeService;


/**
 * 人员 excel 导入
 * @author cc
 *
 */
public class EmployeeExcelListener extends MyExcelListener<EmployeeImportDetailVo> {
	
	private PostService postService = SpringUtils.getBean(PostService.class);
	private DeptService deptService = SpringUtils.getBean(DeptService.class);
	private DictService dictService = SpringUtils.getBean(DictService.class);
	private IEmployeeService employeeService = SpringUtils.getBean(IEmployeeService.class);
	
	private String batchNo = null;
    public EmployeeExcelListener(String batchNo) {
    	this.batchNo = batchNo;
    }
    
    @Override
    public void doValidate(EmployeeImportDetailVo data, AnalysisContext context) {
    	//1.设置批次号
    	//2.校验数据 与 匹配数据字典数据 设置对应字段
    	//3.设置 处理状态 与 处理信息
    	List<String> validateList = new ArrayList<>();
    	
    	data.setBatchNo(this.batchNo);
    	
    	if(StringUtils.isEmpty(data.getEmployeeCode())) {
    		validateList.add("[ 人员编码 ] 为空");
    	} 
    	
    	if(StringUtils.isEmpty(data.getEmployeeName())) {
    		validateList.add("[ 姓名 ] 为空");
    	}
    	
    	if(StringUtils.isEmpty(data.getIdentityId())) {
    		validateList.add("[ 身份证号 ] 为空");
    	}
    	
    	if (!StringUtils.isEmpty(data.getDeptImport())) {
    		long deptId = NumberUtils.toLong(this.deptService.selectDeptIdByName(data.getDeptImport()) , -1);
    		if (deptId == -1) {
    			validateList.add("[ 服务部门 ] 匹配不到数据");
    		} else {
    			data.setDeptId(deptId);
    		}
    	} else {
    		validateList.add("[ 服务部门 ] 为空");
    	}
     
    	if (!StringUtils.isEmpty(data.getServiceTypeImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("erecord_service_type", data.getServiceTypeImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 服务类型 ] 匹配不到数据");
    		} else {
    			data.setServiceType(val);
    		}
    	} 
    	
    	if (!StringUtils.isEmpty(data.getPostImport())) {
    		long postId = NumberUtils.toLong(this.postService.selectPostIdByName(data.getPostImport()) , -1);
    		if (postId == -1) {
    			validateList.add("[ 岗位 ] 匹配不到数据");
    		} else {
    			data.setPostId(postId);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getSexImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("sys_user_sex", data.getSexImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 性别 ] 匹配不到数据");
    		} else {
    			data.setSex(val);
    		}
    	} else {
    		validateList.add("[ 性别 ] 为空");
    	}
    	
    	if (!StringUtils.isEmpty(data.getEducationImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("employee_user_education", data.getEducationImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 学历 ] 匹配不到数据");
    		} else {
    			data.setEducation(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getSocialInsuranceImport())) {
    		String val = this.dictService.getDictValue("common_status_have_or_not", data.getSocialInsuranceImport()); 
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 社保 ] 匹配不到数据");
    		} else {
    			data.setSocialInsurance(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getAccidentInsuranceImport())) {
    		String val = this.dictService.getDictValue("common_status_have_or_not", data.getAccidentInsuranceImport()); 
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 意外险 ] 匹配不到数据");
    		} else {
    			data.setAccidentInsurance(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getEmployeeStatusImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("employee_user_status", data.getEmployeeStatusImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 员工状态 ] 匹配不到数据");
    		} else {
    			data.setEmployeeStatus(val);
    		}
    	}
    	
    	if (!validateList.isEmpty()) {
    		String errMsg = String.join(" ，", validateList);
    		excelResult.getValidateList().add(errMsg);
    		
    		data.setProcessMsg(errMsg);
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
    	} else {
    		data.setProcessMsg("成功");
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
    	}
    }

	@Override
	protected void doOnExceptionProcess(String errorMsg) {
		EmployeeImportDetailVo vo = new EmployeeImportDetailVo();
		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);
	}
}
